package com.cskaoyan.service.impl;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.MarketSystemExample;
import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangsenqi
 * @Description
 * @Create 2022-06-27 9:21
 * @Version
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public Map<String, String> configMall() {

        ArrayList<String> keyNameList = new ArrayList<>();
        keyNameList.add("market_mall_address");
        keyNameList.add("market_mall_latitude");
        keyNameList.add("market_mall_longitude");
        keyNameList.add("market_mall_name");
        keyNameList.add("market_mall_phone");
        keyNameList.add("market_mall_qq");


        return ConfigInfo(keyNameList);
    }

    @Override
    public void changeMall(Map<String, String> map) {

        ConfigChange(map);
    }

    @Override
    public Map<String, String> configExpress() {

        ArrayList<String> keyNameList = new ArrayList<>();
        keyNameList.add("market_express_freight_min");
        keyNameList.add("market_express_freight_value");


        return ConfigInfo(keyNameList);
    }

    @Override
    public void changeExpress(Map<String, String> map) {
        ConfigChange(map);
    }

    @Override
    public Map<String, String> configOrder() {
        ArrayList<String> keyNameList = new ArrayList<>();
        keyNameList.add("market_order_unconfirm");
        keyNameList.add("market_order_unpaid");
        keyNameList.add("market_order_comment");


        return ConfigInfo(keyNameList);
    }

    @Override
    public void changeOrder(Map<String, String> map) {
        ConfigChange(map);
    }

    @Override
    public Map<String, String> configWx() {
        ArrayList<String> keyNameList = new ArrayList<>();
        keyNameList.add("market_wx_index_new");
        keyNameList.add("market_wx_index_topic");
        keyNameList.add("market_wx_share");
        keyNameList.add("market_wx_index_brand");
        keyNameList.add("market_wx_catlog_goods");
        keyNameList.add("market_wx_catlog_list");
        keyNameList.add("market_wx_index_new");
        keyNameList.add("market_wx_index_hot");

        return ConfigInfo(keyNameList);
    }

    @Override
    public void changeWx(Map<String, String> map) {
        ConfigChange(map);
    }


    /**
     * 用于显示配置信息的方法
     *
     * @param keyNameList
     * @return
     */
    private HashMap<String, String> ConfigInfo(ArrayList<String> keyNameList) {
        MarketSystemExample example = new MarketSystemExample();

        MarketSystemExample.Criteria criteria = example.createCriteria();

        criteria.andKeyNameIn(keyNameList);

        List<MarketSystem> marketSystemList = marketSystemMapper.selectByExample(example);

        HashMap<String, String> map = new HashMap<>();

        for (MarketSystem marketSystem : marketSystemList) {
            map.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        }

        return map;
    }

    /**
     * 用于修改配置信息的方法
     *
     * @param map
     */
    private void ConfigChange(Map<String, String> map) {
        for (String key : map.keySet()) {

            MarketSystem marketSystem = new MarketSystem();
            marketSystem.setKeyName(key);
            marketSystem.setKeyValue(map.get(key));

            MarketSystemExample example = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = example.createCriteria();
            criteria.andKeyNameEqualTo(key);

            marketSystemMapper.updateByExampleSelective(marketSystem, example);
        }
    }
}
